package controller

import (
	"gin_server/internal/service"
	"gin_server/pkg/response"

	"github.com/gin-gonic/gin"
)

type UploadFileResponse struct {
	Key              string `json:"key"`
	OriginalFileName string `json:"original_file_name"`
}

type UploadController struct {
	uploadService service.UploadService
}

func NewUploadController(uploadService service.UploadService) *UploadController {
	return &UploadController{
		uploadService: uploadService,
	}
}

// UploadFile 单文件上传
// @Summary 单文件上传
// @Description 上传单个文件到服务器
// @Tags 文件上传
// @Accept multipart/form-data
// @Produce json
// @Param file formData file true "要上传的文件"
// @Success 200 {object} response.Response{data=UploadFileResponse}
// @Security BearerAuth
// @Router /upload/file [post]
func (c *UploadController) UploadFile(ctx *gin.Context) {
	// 获取上传的文件
	file, err := ctx.FormFile("file")
	if err != nil {
		response.BadRequest(ctx, "获取文件失败: "+err.Error())
		return
	}

	// 获取上传的文件key
	fileKey := ctx.PostForm("key")
	if fileKey == "" {
		response.BadRequest(ctx, "文件key不能为空")
		return
	}

	// 调用服务层处理文件上传
	_fileKey, err := c.uploadService.SaveFile(file, fileKey)
	if err != nil {
		response.Error(ctx, "文件上传失败: "+err.Error())
		return
	}

	response.SuccessWithMessage(ctx, "文件上传成功", UploadFileResponse{
		Key:              _fileKey,
		OriginalFileName: file.Filename,
	})
}
